<?php
namespace Usap\Service\Account;

class wyyun{
	
	
	
	public function __construct($appKey, $appSecret){
	
		$this->AppKey = $appKey;
		$this->AppSecret = $appSecret;
		$this->getStatusUrl = 'https://api.netease.im/sms/querystatus.action';
	}
	
	
	/**
	 * 获取短信发送状态
	 * @param unknown $send_id
	 */
	public function getSendStatus($message_sid){
		
		$CurTime = time();//当前时间戳
		$Nonce = uniqid().substr(microtime(true),-3);
		$CheckSum = sha1($this->AppSecret.$Nonce.$CurTime);
		
		$cmd = '/usr/bin/curl -X POST -H "AppKey: '.$this->AppKey.'" -H "CurTime: '.$CurTime.'" -H "CheckSum: '.$CheckSum.'" -H "Nonce: '.$Nonce.'" -H "charset: utf-8"  -H "Content-Type: application/x-www-form-urlencoded" -d "sendid='.$message_sid.'"' . ' "'.$this->getStatusUrl.'"';
		exec($cmd, $results, $return);
		$respond_info = json_decode($results[0],true);
		
		$status_code = $respond_info['code'];
		
		if($status_code!='200'){
			
			return $this->returnMsg($status_code);
		}
		
		return array(
			'code'=>'0000','msg'=>'查询成功',
			'status'=>$respond_info['obj'][0]['status'],
			'mobile'=>$respond_info['obj'][0]['mobile']
		);
	}
	
	
	/**
	 * 发送模版短信
	 * @param unknown $to
	 * @param unknown $datas
	 * @param unknown $tempId
	 */
	public function sendTemplateSMS($to, $datas, $tempId){
    
		if(is_array($to)){

			$to = "'".implode("','",$to)."'" ;
		}else{
			$to = str_replace(array(',',"\""), '', $to);
			$to = "'".str_replace(",", "','", $to)."'";
		}
		
		
		if(is_array($datas)){

			$datas = "'".implode("','",$datas)."'";
		}else{
			$datas = str_replace(array(',',"\""), '', $datas);
			$datas  = "'".str_replace(",", "','", $datas)."'";
		}

		$CurTime = time();//当前时间戳
		$Nonce = uniqid().substr(microtime(true),-3);
		$CheckSum = sha1($this->AppSecret.$Nonce.$CurTime);

		$cmd = '/usr/bin/curl -X POST -H "AppKey: '.$this->AppKey.'" -H "CurTime: '.$CurTime.'" -H "CheckSum: '.$CheckSum.'" -H "Nonce: '.$Nonce.'" -H "charset: utf-8"  -H "Content-Type: application/x-www-form-urlencoded" -d "templateid='.$tempId.'&mobiles=['.$to.']&params=['.$datas.']"' . ' "https://api.netease.im/sms/sendtemplate.action"';
		exec($cmd, $results, $return);
		$respond_info = json_decode($results[0],true);
		$code = $respond_info['code'];
		
		$msg_info = $this->returnMsg($code);
		
		
		if($msg_info['code']=='200'){

			$msg_code = '0000';
		}else{
			$msg_code = $msg_info['code'];
		}
		
		$datas = new \stdClass();
		$datas->statusCode = $msg_code;
		$datas->statusMsg = $msg_info['msg'];
		$datas->smsMessageSid = $respond_info['obj'];
		return $datas;
    }
    
    
    public function returnMsg($code){
    	
    	$cfg = array(
	    	"200"=>"操作成功",
	    	"201"=>"客户端版本不对，需升级sdk",
	    	"301"=>"被封禁",
	    	"302"=>"用户名或密码错误",
	    	"315"=>"IP限制",
	    	"403"=>"非法操作或没有权限",
	    	"404"=>"对象不存在",
	    	"405"=>"参数长度过长",
	    	"406"=>"对象只读",
	    	"408"=>"客户端请求超时",
	    	"413"=>"验证失败(短信服务)",
	    	"414"=>"参数错误",
	    	"415"=>"客户端网络问题",
	    	"416"=>"频率控制",
	    	"417"=>"自动登录失效",
	    	"418"=>"通道不可用(短信服务)",
	    	"419"=>"数量超过上限",
	    	"422"=>"账号被禁用",
	    	"431"=>"HTTP重复请求",
	    	"500"=>"服务器内部错误",
	    	"503"=>"服务器繁忙",
	    	"514"=>"服务不可用",
	    	"509"=>"无效协议",
	    	"998"=>"解包错误",
	    	"999"=>"打包错误",
	    	"801"=>"群人数达到上限",
	    	"802"=>"没有权限",
	    	"803"=>"群不存在",
	    	"804"=>"用户不在群",
	    	"805"=>"群类型不匹配",
	    	"806"=>"创建群数量达到限制",
	    	"807"=>"群成员状态错误",
	    	"808"=>"申请成功",
	    	"809"=>"已经在群内",
	    	"810"=>"邀请成功",
	    	"9102"=>"通道失效",
	    	"9103"=>"已经在他端对这个呼叫响应过了",
	    	"11001"=>"通话不可达，对方离线状态",
	    	"13001"=>"IM主连接状态异常",
	    	"13002"=>"聊天室状态异常",
	    	"13003"=>"账号在黑名单中,不允许进入聊天室",
	    	"13004"=>"在禁言列表中,不允许发言",
	    	"10431"=>"输入email不是邮箱",
	    	"10432"=>"输入mobile不是手机号码",
	    	"10433"=>"注册输入的两次密码不相同",
	    	"10434"=>"企业不存在",
	    	"10435"=>"登陆密码或帐号不对",
	    	"10436"=>"app不存在",
	    	"10437"=>"email已注册",
	    	"10438"=>"手机号已注册",
	    	"10441"=>"app名字已经存在"
    	);
    	return isset($cfg[$code]) ? array('code'=>$code,'msg'=>$cfg[$code]) : array('code'=>'-9999','msg'=>'系统异常'); 
    }
}
